package org.gangcai.mac.shop.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.api.AppAPI;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.UserDataBean;
import org.gangcai.mac.shop.bean.UserMessageBean;
import org.gangcai.mac.shop.common.BaseActivity;
import org.gangcai.mac.shop.common.BaseApplication;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.manager.RetrofitManageres;
import org.gangcai.mac.shop.oberver.CommonObserver;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;

import static org.gangcai.mac.shop.constants.Constant.CODE_SUCC;

public class RegisterActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {

    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.iphone)
    EditText iphone;
    @BindView(R.id.code)
    EditText code;
    @BindView(R.id.rightTopTv)
    TextView rightTopTv;
    @BindView(R.id.newPassword)
    EditText newPassword;
    @BindView(R.id.newPassword2)
    EditText newPassword2;
    @BindView(R.id.protocolCb)
    CheckBox protocol;
    @BindView(R.id.btn)
    Button btn;
    @BindView(R.id.protocolTv)
    TextView protocolTv;
    @BindView(R.id.protocoLl)
    LinearLayout protocoLl;
    @BindView(R.id.registerCode)
    EditText registerCode;
    @BindView(R.id.forgetPasswordsolide)
    View forgetPasswordsolide;

    private boolean isCheck = false;
    public String codeType = "1";
    private long timeCount = 60 * 1000;

    public String codemsg = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        initTitleBarView(titlebar, "注册");
        initView();
    }

    private void initView() {
        SpannableString spannableString = new SpannableString("我同意<<注册协议>>");
        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimary)), 3, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        protocolTv.setText(spannableString);
        btn.setText("注册");
        protocol.setOnCheckedChangeListener(this);
    }

    @OnClick({R.id.protocolTv, R.id.rightTopTv, R.id.btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rightTopTv:

                rightTopTv.setEnabled(false);
                rightTopTv.setText("发送中...");
                requestCode(rightTopTv);

                break;
            case R.id.protocolTv:
//                ActivityUtils.startActivity();
                ToastUtils.showShort("滴滴滴");
                break;
            case R.id.btn:
               /* if ("1".equals(codeType) && !isCheck) {
                    ToastUtils.showShort("请先同意注册协议");
                    return;
                }*/
                getObserver()
                        .compose(this.<UserDataBean>bindUntilOnDestoryEvent())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new CommonObserver<UserDataBean>(prompDialog) {
                            @Override
                            public void onSuccess(UserDataBean value) {
                                if (value.getCode() == CODE_SUCC) {
                                    if ("1".equals(codeType)) {
                                        ToastUtils.showShort("注册成功,请登录");
                                        BaseApplication.getInstance().setLoginUser(value.getReferer());
                                        SPUtils.getInstance().put("uid", value.getReferer().getId());
                                        SPUtils.getInstance().put("phoneNumber", iphone.getText().toString());
                                        JPushInterface.setAlias(RegisterActivity.this, 1, value.getUid());
                                    } else {
                                        ToastUtils.showShort("更改密码成功,请登录");
                                    }
                                    finish();
                                }else{
                                    ToastUtils.showShort(value.getInfo());
                                }
                            }
                        });
                break;
                default:break;
        }
    }

    public Observable<UserDataBean> getObserver() {
        Observable<UserDataBean> register = RetrofitManageres.getInstance().create(AppAPI.class)
                .RegisterData(iphone.getText().toString(),
                        newPassword.getText().toString(),
                        code.getText().toString());
        return register;
    }

    private void requestCode(final TextView rightTopTv) {

        RetrofitManageres.getInstance().create(AppAPI.class)
                .SendCode(iphone.getText().toString())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<UserMessageBean>(prompDialog) {
                    @Override
                    public void onSuccess(UserMessageBean value) {
                        if (value.getCode() == CODE_SUCC) {
                            codemsg=value.getCodemsg();
                            beginCountDown(rightTopTv);
                        } else {
                            rightTopTv.setEnabled(true);
                            rightTopTv.setText("获取验证码");
                        }
                    }

                    @Override
                    public boolean onFailure(Throwable e) {
                        rightTopTv.setText("获取验证码");
                        rightTopTv.setEnabled(true);
                        return super.onFailure(e);
                    }
                });


    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        this.isCheck = isChecked;
    }

    private void beginCountDown(final TextView tv) {
        CountDownTimer countDownTimer = new CountDownTimer(timeCount, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                int i = (int) (millisUntilFinished + 500) / 1000;
                tv.setText(i + "s");
            }

            @Override
            public void onFinish() {
                tv.setText("重新获取");
                tv.setEnabled(true);
            }
        }.start();
    }
}
